package com.sh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.bean.Resfood;
import com.sh.dao.ResfoodMapper;
import com.sh.service.ResfoodBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
@Slf4j
public class ResfoodBizImpl implements ResfoodBiz {
    @Autowired
    private ResfoodMapper resfoodDao;
    @Value("${spring.cache.cacheName}")
    private String cacheName;


    @Cacheable(cacheNames = "#{@resfoodBizImpl.cacheName}", key = "'all'")
    @Override
    public List<Resfood> findAll() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("fid");
        return this.resfoodDao.selectList(wrapper);
    }

    @Cacheable(cacheNames = "#{@resfoodBizImpl.cacheName}", key = "'#fid'")
    @Override
    public Resfood findById(Integer id) {
        return this.resfoodDao.selectById(id);
    }

    @Cacheable(cacheNames = "#{@resfoodBizImpl.cacheName}", key = "#pageno + '-' + #pagesize + '-' + #sortby + '-' + #sort")
    @Override
    public Page<Resfood> findFoods(Resfood food, int pageNo, int pageSize, String sortby, String sort) {
        QueryWrapper wrapper = new QueryWrapper();
        if (food != null) {
          if ( food.getFname() !=null && !"".equals(food.getFname())){
              wrapper.like("fname", food.getFname());
          }
          if ( food.getDetail() !=null && !"".equals(food.getDetail())){
              wrapper.eq("detail", food.getDetail());
          }
        }
        return findFoods( wrapper, pageNo, pageSize, sortby, sort );
    }

    // 分页查询公共部分
    public Page<Resfood> findFoods(QueryWrapper wrapper, int pageNo, int pageSize, String sortby, String sort) {
        if ( "asc".equalsIgnoreCase(sort) ){
            wrapper.orderByAsc(sortby);
        }else {
            wrapper.orderByDesc(sortby);
        }
        Page<Resfood> page = new Page<>(pageNo, pageSize);
        Page<Resfood> resfoodPage = this.resfoodDao.selectPage(page, wrapper);
        return resfoodPage;
    }

    @Cacheable(cacheNames = "#{@resfoodBizImpl.cacheName}", key = "#pageno + '-' + #pagesize + '-' + #sortby + '-' + #sort")
    @Override
    public Page<Resfood> findByPage(int pageNo, int pageSize, String sortby, String sort) {
        QueryWrapper wrapper = new QueryWrapper<>();
        return findFoods(wrapper, pageNo, pageSize, sortby, sort);
    }


    @CacheEvict(cacheNames = "#{@resfoodBizImpl.cacheName}", allEntries = true)  // 添加数据后清除所有缓存
    @Transactional(
            propagation = Propagation.SUPPORTS,
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            readOnly = false,
            rollbackFor = RuntimeException.class
    )
    @Override
    public Integer addResfood(Resfood food) {
        this.resfoodDao.insert(food);
        return food.getFid();
    }
}
